What is stat-mode?
The stat-mode npm package is used to interpret and manipulate the file mode (permissions) of a file system's stat object. It provides a simple interface to read and modify the permissions of files and directories.
What are stat-mode's main functionalities?
Reading File Permissions
This feature allows you to read the permissions of a file. The code sample demonstrates how to use the stat-mode package to check if the owner can read, the group can write, and others can execute the file 'example.txt'.
const fs = require('fs');
const Mode = require('stat-mode');
fs.stat('example.txt', (err, stats) => {
if (err) throw err;
const mode = new Mode(stats);
console.log(`Owner can read: ${mode.owner.read}`);
console.log(`Group can write: ${mode.group.write}`);
console.log(`Others can execute: ${mode.others.execute}`);
});
Modifying File Permissions
This feature allows you to modify the permissions of a file. The code sample shows how to use the stat-mode package to set the read permission for the owner, write permission for the group, and execute permission for others on the file 'example.txt'.
const fs = require('fs');
const Mode = require('stat-mode');
fs.stat('example.txt', (err, stats) => {
if (err) throw err;
const mode = new Mode(stats);
mode.owner.read = true;
mode.group.write = true;
mode.others.execute = true;
fs.chmod('example.txt', mode.stat.mode, (err) => {
if (err) throw err;
console.log('Permissions updated');
});
});
Other packages similar to stat-mode
fs-extra
fs-extra is a package that extends the native Node.js fs module with additional methods and promises. It includes methods for reading and writing file permissions, similar to stat-mode, but also provides a broader range of file system operations such as copying, moving, and removing files and directories.
chmodr
chmodr is a package specifically designed for recursively changing file permissions. While stat-mode focuses on interpreting and modifying file modes, chmodr provides a simple interface for changing permissions of entire directory trees, making it more suitable for bulk permission changes.
stat-mode
Offers convenient getters and setters for the stat mode
You know that mode
property on the fs.Stat
object that you probably
usually just ignore? Well there's acutally a lot of information packed
into that number.
The specific information includes:
This module helps you extract that information.
All the getters are also setters, which change the mode
property
appropriately. This is useful for when you have to build up your
own fs.Stat
object for whatever reason (like when implementing a
FUSE filesystem.
Installation
$ npm install stat-mode
Example
So given some arbitrary file (let's say /bin/echo
):
$ ls -l /bin/echo
-rwxr-xr-x 1 root wheel 14128 Aug 11 2013 /bin/echo
We can inspect it using the fs.stat()
call and creating a Mode
instance
on top of it.
var fs = require('fs');
var Mode = require('stat-mode');
fs.stat('/bin/echo', function (err, stat) {
if (err) throw err;
var mode = new Mode(stat);
mode.isDirectory();
mode.isFIFO();
mode.isFile();
mode.owner.read;
mode.owner.write;
mode.owner.execute;
mode.group.read;
mode.group.write;
mode.group.execute;
mode.others.read;
mode.others.write;
mode.others.execute;
mode.toString();
});
API
new Mode(Object stat) → Mode
You must pass in "stat" object to the Mode
constructor. The "stat"
object can be a real fs.Stat
instance, or really any Object with a
mode
property.
mode.isDirectory([Boolean set]) → Boolean
Returns true
if the mode's file type is "directory", false
otherwise.
If you pass true
to the function, then the mode will be set to "directory".
mode.isFile([Boolean set]) → Boolean
Returns true
if the mode's file type is "file", false
otherwise.
If you pass true
to the function, then the mode will be set to "file".
mode.isBlockDevice([Boolean set]) → Boolean
Returns true
if the mode's file type is "block device", false
otherwise.
If you pass true
to the function, then the mode will be set to "block device".
mode.isCharacterDevice([Boolean set]) → Boolean
Returns true
if the mode's file type is "character device", false
otherwise.
If you pass true
to the function, then the mode will be set to "character
device".
mode.isSymbolicLink([Boolean set]) → Boolean
Returns true
if the mode's file type is "symbolic link", false
otherwise.
If you pass true
to the function, then the mode will be set to "symbolic link".
mode.isFIFO([Boolean set]) → Boolean
Returns true
if the mode's file type is "FIFO", false
otherwise.
If you pass true
to the function, then the mode will be set to "FIFO".
mode.isSocket([Boolean set]) → Boolean
Returns true
if the mode's file type is "socket", false
otherwise.
If you pass true
to the function, then the mode will be set to "socket".
mode.owner.read → Boolean [Getter/Setter]
true
if the mode is "owner read" rights, false
otherwise.
mode.owner.write → Boolean [Getter/Setter]
true
if the mode is "owner write" rights, false
otherwise.
mode.owner.execute → Boolean [Getter/Setter]
true
if the mode is "owner execute" rights, false
otherwise.
mode.group.read → Boolean [Getter/Setter]
true
if the mode is "group read" rights, false
otherwise.
mode.group.write → Boolean [Getter/Setter]
true
if the mode is "group write" rights, false
otherwise.
mode.group.execute → Boolean [Getter/Setter]
true
if the mode is "group execute" rights, false
otherwise.
mode.others.read → Boolean [Getter/Setter]
true
if the mode is "others read" rights, false
otherwise.
mode.others.write → Boolean [Getter/Setter]
true
if the mode is "others write" rights, false
otherwise.
mode.others.execute → Boolean [Getter/Setter]
true
if the mode is "others execute" rights, false
otherwise.